from models.meta import BaseDocument, IdField, fields


class ScanTasks(BaseDocument):
    task_id = IdField()
    target_id = IdField()
    user_id = IdField()
    last_task_session_id = IdField()
    next_start_at = fields.DateTimeField()
    last_started_at = fields.DateTimeField()
    status = fields.IntField()
    addition = fields.DictField(default={})
    progress = fields.FloatField(default=0)
    status_name = fields.StringField(default='')
    job_config_map = fields.DictField()
    task_config = fields.DictField()

    meta = {
        'collection': 'scan_tasks',
        'primary_key': 'task_id',
        'indexes': ['addition.task_type'],
    }


class ScanTaskSessions(BaseDocument):
    task_id = IdField()
    target_url = fields.StringField()
    target_id = IdField()
    user_id = IdField()
    task_session_id = IdField()
    addition = fields.DictField()
    started_at = fields.DateTimeField()
    ended_at = fields.DateTimeField()
    task_config = fields.DictField()
    job_config_map = fields.DictField()
    status_name = fields.StringField()
    status = fields.StringField()
    progress = fields.FloatField(default=0)
    job_names = fields.ListField()

    meta = {
        'collection': 'scan_task_sessions',
        'primary_key': 'task_session_id',
        'indexes': ['addition.task_type', ('target_id', 'addition.task_type', 'status')],
    }


class ScanEvents(BaseDocument):
    meta = {'collection': 'scan_events', 'primary_key': 'event_id'}

    event_id = IdField()
    target_id = IdField()
    user_id = IdField()
    task_session_id = IdField()
    target_url = fields.StringField()
    detail = fields.DictField()
    event_name = fields.StringField()
    severity = fields.IntField()
    event_type = fields.IntField()
    job_name = fields.StringField()


class ScanPages(BaseDocument):
    meta = {
        'collection': 'scan_pages',
    }

    task_id = IdField()
    task_session_id = IdField()
    detail = fields.DictField()


class ScanEventActions(BaseDocument):
    meta = {
        'collection': 'scan_event_actions',
        'primary_key': 'event_id',
        'indexes': ['event_id', 'action', 'verification', 'target_id', 'target_url'],
    }

    event_id = IdField(required=True, unique=True)
    target_id = IdField()
    user_id = IdField()
    target_url = fields.StringField()

    # 0 not verified, 1 verifying, 2 verified true, 3 verified false 4 verified again
    verification = fields.IntField(default=0, choices=[0, 1, 2, 3, 4])
    action = fields.IntField(
        default=0, choices=[0, 1, 2]
    )  # 0 normal, 1 fixed, 2 ignored

    verification_detail = fields.DictField()


class ScanTaskSessionsExtra(BaseDocument):
    task_session_id = IdField()
    target_id = IdField()
    user_id = IdField()
    data_version = fields.StringField()
    system_version = fields.StringField()
    status = fields.IntField()
    status_name = fields.StringField()
    reason = fields.StringField()

    meta = {
        'collection': 'scan_task_sessions_extra',
        'primary_key': 'task_session_id',
        'indexes': ['task_session_id', 'target_id', 'user_id'],
    }
